var menuIndex=0;
var oriDataLength=0;
colorAdminApp.directive('repeatFinish',function(){
    return {
        link: function(scope,element,attr){
        	menuIndex=menuIndex+1;
            if(menuIndex==oriDataLength){
               	setTimeout("App.init()",2000);//ng-repeat 完成之后要初始化一下菜单
            	menuIndex=0;
            }
        }
    }
})
colorAdminApp.controller('indexController', function($scope,$rootScope,$http,$state,$stateParams) {
	
	 // $state.go('index.errors',{"code":403});
	
	 /**
	  * 实现页面路由
	  */
	  $scope.includePage=function(url,name){
			
	  };
	
	  $scope.goState=function(stateCode,name){
		$state.go(stateCode,{"name":name});
	  };
	
	  $scope.logout=function(){
		var entity={};
		$http.post('logout',{params:entity}).success(function(data, status, headers, config) {
		   if(status==200){
			   $state.go("login");
		   }
		}).error(function(data, status, headers, config) {
	    	alert(data);
        });  
	 }
	
	
		$scope.initTree=function initTree(){
			var entity={};
			$http.post('core/resources/getUserMenuTreeJSON.htm',{params:entity}).success(function(data, status, headers, config) {
				oriDataLength=data.length; 
				$scope.items=$scope.convertData(data,0);
			}).error(function(data, status, headers, config) {
		    	alert(data);
	        });  
		};
		
	
		/**
		 * 转换菜单数据格式以便实现递归
		 */
		$scope.convertData=function(list,parentId){
			var retList=[];
			for(var i=0;i<list.length;i++){
				var t=list[i];
				if(t.pId==parentId){
					var children=$scope.convertData(list,t.id);
					if(children.length>0){
						t["hasSub"]=true;
					}else{
						t["hasSub"]=false;
					}
					t["children"]=children;
					retList.push(t);
				}
			}
			return retList;
		}
		
	    $scope.initTree(); 
	    
		
});


